package com.jrx.anytxn.reporting.job.xib.config;

import com.alibaba.fastjson.JSONObject;
import com.jrx.anytxn.param.entity.PrOrganizationTable;
import com.jrx.anytxn.param.service.system.IOrganizationTableService;
import com.jrx.anytxn.param.utils.BizTypeProducts;
import com.jrx.anytxn.reporting.config.AnyTxnBatchProperties;
import com.jrx.anytxn.reporting.constant.ReportingConstant;
import com.jrx.anytxn.reporting.entity.AmRepaymentPlanInfo;
import com.jrx.anytxn.reporting.entity.CmInteCoLonInfoCi;
import com.jrx.anytxn.reporting.entity.CmInteCoLonRepayDtlCi;
import com.jrx.anytxn.reporting.entity.CmInteCoLonRepayPlanCi;
import com.jrx.anytxn.reporting.job.xib.step.FtpInteCoLonListener;
import com.jrx.anytxn.reporting.job.xib.step.LoanInfoCiProcessor;
import com.jrx.anytxn.reporting.job.xib.step.LoanInfoCiWriter;
import com.jrx.anytxn.reporting.job.xib.step.RepayDtlCiProcessor;
import com.jrx.anytxn.reporting.job.xib.step.RepayDtlCiWriter;
import com.jrx.anytxn.reporting.job.xib.step.RepayPlanCiProcessor;
import com.jrx.anytxn.reporting.job.xib.step.RepayPlanCiWriter;
import com.jrx.anytxn.transaction.constant.TransBizConstant;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.batch.MyBatisCursorItemReader;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepScope;
import org.springframework.batch.core.launch.support.RunIdIncrementer;
import org.springframework.batch.item.support.SynchronizedItemStreamReader;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.task.SimpleAsyncTaskExecutor;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @comment: 免息成本报表任务定义
 * @author: liuzb
 * @date: 2021/9/22 15:00
 */
@Configuration
@EnableConfigurationProperties(AnyTxnBatchProperties.class)
@EnableBatchProcessing
public class FtpInteCoLonJobConfiguration {

    @Resource(name = "bizSqlSessionFactory")
    SqlSessionFactory sqlSessionFactory;
    @Autowired
    public JobBuilderFactory jobBuilderFactory;
    @Autowired
    public StepBuilderFactory stepBuilderFactory;
    @Autowired
    AnyTxnBatchProperties anyTxnBatchProperties;
    @Autowired
    FtpInteCoLonListener ftpInteCoLonListener;
    @Autowired
    RepayPlanCiProcessor repayPlanCiProcessor;
    @Autowired
    RepayPlanCiWriter repayPlanCiWriter;
    @Autowired
    RepayDtlCiProcessor repayDtlCiProcessor;
    @Autowired
    RepayDtlCiWriter repayDtlCiWriter;
    @Autowired
    LoanInfoCiProcessor loanInfoCiProcessor;
    @Autowired
    LoanInfoCiWriter loanInfoCiWriter;
    @Autowired
    private IOrganizationTableService organizationTableService;

    @Bean
    public Job ftpInteCoLonJob() {
        return jobBuilderFactory.get("ftpInteCoLonJob")
                .incrementer(new RunIdIncrementer())
                .listener(ftpInteCoLonListener)// 清空当天的批次
                .start(repayPlanCiStep())//贷款还款计划FTP供数
                .next(repayDtlCiStep())//贷款还款明细FTP供数
                .next(loanInfoCiStep())//贷款信息FTP供数
                .build();
    }


    /**
     * =======================================================================================================================================
     * 贷款还款计划FTP供数
     *
     * @return
     */
    @Bean
    public Step repayPlanCiStep() {
        return stepBuilderFactory.get("repayPlanCiStep")
                .<AmRepaymentPlanInfo, CmInteCoLonRepayPlanCi>chunk(anyTxnBatchProperties.getReportingCustomerConfig().getChunkLimit())
                .reader(repayPlanReader())
                .processor(repayPlanCiProcessor)
                .writer(repayPlanCiWriter)
                .taskExecutor(new SimpleAsyncTaskExecutor())
                .throttleLimit(anyTxnBatchProperties.getReportingCustomerConfig().getThrottleLimit())
                .build();
    }

    @Bean
    @StepScope
    public SynchronizedItemStreamReader<AmRepaymentPlanInfo> repayPlanReader() {
        PrOrganizationTable org = organizationTableService.findByOrgIdForBatch(ReportingConstant.DEFAULT_ORG_NUMBER);
        MyBatisCursorItemReader<AmRepaymentPlanInfo> cursorItemReader = new MyBatisCursorItemReader<>();
        cursorItemReader.setSaveState(false);
        cursorItemReader.setSqlSessionFactory(sqlSessionFactory);
        //兼容信用就医 用getAllRepayPlanCiWithProducts替换getAllRepayPlanCi
        cursorItemReader.setQueryId("com.jrx.anytxn.reporting.mapper.ext.ExtFtpInteCoLonMapper.getAllRepayPlanCiWithProducts");
        Map<String, Object> map = new HashMap<>();
        map.put("accountStatus", Arrays.asList(ReportingConstant.ACCOUNT_STATUS_1, ReportingConstant.ACCOUNT_STATUS_2));
        map.put("payOffStatus", Arrays.asList(ReportingConstant.ACCOUNT_STATUS_6, ReportingConstant.ACCOUNT_STATUS_7, ReportingConstant.ACCOUNT_STATUS_8));
        //map.put("loanType", ReportingConstant.LOAN_TYPE_4);
        map.put("today", org.getToday());
        map.put("productIds", BizTypeProducts.getXAHAndXAJSeriesProducts());
        cursorItemReader.setParameterValues(map);
        SynchronizedItemStreamReader<AmRepaymentPlanInfo> synchronizedItemStreamReader = new SynchronizedItemStreamReader<>();
        synchronizedItemStreamReader.setDelegate(cursorItemReader);
        return synchronizedItemStreamReader;
    }

    /**
     * =======================================================================================================================================
     * 贷款还款明细FTP供数
     *
     * @return
     */
    @Bean
    public Step repayDtlCiStep() {
        return stepBuilderFactory.get("repayDtlCiStep")
                .<CmInteCoLonRepayDtlCi, CmInteCoLonRepayDtlCi>chunk(anyTxnBatchProperties.getReportingCustomerConfig().getChunkLimit())
                .reader(repayDtlCiReader())
                .processor(repayDtlCiProcessor)
                .writer(repayDtlCiWriter)
                .taskExecutor(new SimpleAsyncTaskExecutor())
                .throttleLimit(anyTxnBatchProperties.getReportingCustomerConfig().getThrottleLimit())
                .build();
    }

    @Bean
    @StepScope
    public SynchronizedItemStreamReader<CmInteCoLonRepayDtlCi> repayDtlCiReader() {
        PrOrganizationTable org = organizationTableService.findByOrgIdForBatch(ReportingConstant.DEFAULT_ORG_NUMBER);
        MyBatisCursorItemReader<CmInteCoLonRepayDtlCi> cursorItemReader = new MyBatisCursorItemReader<>();
        cursorItemReader.setSaveState(false);
        cursorItemReader.setSqlSessionFactory(sqlSessionFactory);
        //兼容信用就医 用getAllRepayDtlCiWithProducts替换getAllRepayDtlCi
        cursorItemReader.setQueryId("com.jrx.anytxn.reporting.mapper.ext.ExtFtpInteCoLonMapper.getAllRepayDtlCiWithProducts");
        Map<String, Object> map = new HashMap<>();
        map.put("today", org.getToday());
        map.put("productIds", BizTypeProducts.getXAHAndXAJSeriesProducts());
        cursorItemReader.setParameterValues(map);
        SynchronizedItemStreamReader<CmInteCoLonRepayDtlCi> synchronizedItemStreamReader = new SynchronizedItemStreamReader<>();
        synchronizedItemStreamReader.setDelegate(cursorItemReader);
        return synchronizedItemStreamReader;
    }

    /**
     * =======================================================================================================================================
     * 贷款还款明细FTP供数
     *
     * @return
     */
    @Bean
    public Step loanInfoCiStep() {
        return stepBuilderFactory.get("loanInfoCiStep")
                .<CmInteCoLonInfoCi, CmInteCoLonInfoCi>chunk(anyTxnBatchProperties.getReportingCustomerConfig().getChunkLimit())
                .reader(loanInfoReader())
                .processor(loanInfoCiProcessor)
                .writer(loanInfoCiWriter)
                .taskExecutor(new SimpleAsyncTaskExecutor())
                .throttleLimit(anyTxnBatchProperties.getReportingCustomerConfig().getThrottleLimit())
                .build();
    }

    @Bean
    @StepScope
    public SynchronizedItemStreamReader<CmInteCoLonInfoCi> loanInfoReader() {
        PrOrganizationTable org = organizationTableService.findByOrgIdForBatch(ReportingConstant.DEFAULT_ORG_NUMBER);
        MyBatisCursorItemReader<CmInteCoLonInfoCi> cursorItemReader = new MyBatisCursorItemReader<>();
        cursorItemReader.setSaveState(false);
        cursorItemReader.setSqlSessionFactory(sqlSessionFactory);
        //信用就医替换 用getAllLoanInfoCiWithProducts 替换 getAllLoanInfoCi
        cursorItemReader.setQueryId("com.jrx.anytxn.reporting.mapper.ext.ExtFtpInteCoLonMapper.getAllLoanInfoCiWithProducts");
        Map<String, Object> map = new HashMap<>();
        map.put("accountStatus", Arrays.asList(ReportingConstant.ACCOUNT_STATUS_1, ReportingConstant.ACCOUNT_STATUS_2));
        map.put("payOffStatus", Arrays.asList(ReportingConstant.ACCOUNT_STATUS_6, ReportingConstant.ACCOUNT_STATUS_7, ReportingConstant.ACCOUNT_STATUS_8));
        map.put("loanTypes", Arrays.asList(TransBizConstant.LOAN_TYPE_3, TransBizConstant.LOAN_TYPE_4, TransBizConstant.LOAN_TYPE_7));
        map.put("today", org.getToday());
        map.put("productIds", BizTypeProducts.getXAHAndXAJSeriesProducts());
        cursorItemReader.setParameterValues(map);
        SynchronizedItemStreamReader<CmInteCoLonInfoCi> synchronizedItemStreamReader = new SynchronizedItemStreamReader<>();
        synchronizedItemStreamReader.setDelegate(cursorItemReader);
        return synchronizedItemStreamReader;
    }
}
